﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLFacade;
using BLFacade.entities;
using System.Data.Objects;
using System.Security.Cryptography;

namespace BL
{
    public class UserFacade: IUserFacade
    {
        Entities entities;
        public UserFacade()
        {
            entities = new Entities();
        }

        public IPerson GetUser(string username, string password)
        {
            if (entities.Persons.Where(p => p.Username == username && p.Password == password).Count() == 0)
                throw new AccessViolationException("Incorrect username or password");
            Person root = entities.Persons.First(p => p.Username == username && p.Password == password);
            if (root.Employees.Count > 0) return root.Employees.First();
            return root.CustomerContacts.First();
        }

        public IPerson CurrentPerson
        {
            get;
            set;
        }

        public bool IsEmployee()
        {
            return CurrentPerson is IEmployee;
        }

        public bool IsHRManager()
        {
            return IsEmployee() ? ((IEmployee)CurrentPerson).Appointment.Name.Equals("HR Manager") : false;
        }

        public bool IsAdministrator()
        {
            return IsEmployee() ? ((IEmployee)CurrentPerson).Appointment.Name.Equals("Administrator") : false;
        }
    }
}
